package com.example.app.init.strategy;

import com.example.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class UserStrategyContext implements CommandLineRunner {

    private final Map<String, UserService> userServiceMap = new HashMap<>();
    @Autowired
    private ApplicationContext applicationContext;

    public UserService getStrategy(String mark){
        return userServiceMap.get(mark);
    }
    @Override
    public void run(String... args) {
        Map<String, UserService> beans = applicationContext.getBeansOfType(UserService.class);
        beans.forEach((key, value) -> userServiceMap.put(value.mark(), value));
    }
}
